package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.tradewar.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;

public class UserTradeWarDaoImpl extends TempDataObjectDao<UserTradeWarBattle> {

	public UserTradeWarBattle getUserTradeWarBattle(long userId) {
		String sql = "select * from t_u_trade_war_battle where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarBattle data = parse(rs);
				return data;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return null;
	}

	@Override
	protected boolean addData(UserTradeWarBattle data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_trade_war_battle(userId,battleType,patronsList,buyBuffTimes,enemyUserId,enemyNickName,enemyTitleId,enemyDecoration,"
				+ "enemyScore,useItemId,battleUid,modifyTime,enemyVipExp,enemyPatronsCount,enemyCity,warTimes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getBattleType());
			call.setString(i++, StringUtils.listToString(data.getPatronsList(), ','));
			call.setInt(i++, data.getBuyBuffTimes());
			call.setLong(i++, data.getEnemyUserId());
			call.setString(i++, data.getEnemyNickName());
			call.setInt(i++, data.getEnemyTitleId());
			call.setString(i++, data.getEnemyDecoration());
			call.setLong(i++, data.getEnemyScore());
			call.setInt(i++, data.getUseItemId());
			call.setString(i++, data.getBattleUid());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getEnemyVipExp());
			call.setInt(i++, data.getEnemyPatronsCount());
			call.setString(i++, data.getEnemyCity());
			call.setInt(i++, data.getWarTimes());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTradeWarBattle data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_trade_war_battle SET battleType=?,patronsList=?,buyBuffTimes=?,enemyUserId=?,enemyNickName=?,enemyTitleId=?,"
				+ "enemyDecoration=?,enemyScore=?,useItemId=?,battleUid=?,modifyTime=?,enemyVipExp=?,enemyPatronsCount=?,enemyCity=?,warTimes=? WHERE userId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getBattleType());
			call.setString(i++, StringUtils.listToString(data.getPatronsList(), ','));
			call.setInt(i++, data.getBuyBuffTimes());
			call.setLong(i++, data.getEnemyUserId());
			call.setString(i++, data.getEnemyNickName());
			call.setInt(i++, data.getEnemyTitleId());
			call.setString(i++, data.getEnemyDecoration());
			call.setLong(i++, data.getEnemyScore());
			call.setInt(i++, data.getUseItemId());
			call.setString(i++, data.getBattleUid());
			call.setLong(i++, data.getModifyTime());
			call.setLong(i++, data.getEnemyVipExp());
			call.setInt(i++, data.getEnemyPatronsCount());
			call.setString(i++, data.getEnemyCity());
			call.setInt(i++, data.getWarTimes());
			call.setLong(i++, data.getUserId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTradeWarBattle parseData(ResultSet rs) throws SQLException {
		UserTradeWarBattle data = new UserTradeWarBattle();
		data.setUserId(rs.getLong("userId"));
		data.setBattleType(rs.getInt("battleType"));
		data.setBuyBuffTimes(rs.getInt("buyBuffTimes"));
		data.setEnemyUserId(rs.getLong("enemyUserId"));
		data.setEnemyNickName(rs.getString("enemyNickName"));
		data.setEnemyTitleId(rs.getInt("enemyTitleId"));
		data.setEnemyDecoration(rs.getString("enemyDecoration"));
		data.setEnemyScore(rs.getLong("enemyScore"));
		data.setEnemyVipExp(rs.getLong("enemyVipExp"));
		data.setEnemyPatronsCount(rs.getInt("enemyPatronsCount"));
		data.setEnemyCity(rs.getString("enemyCity"));
		data.setUseItemId(rs.getInt("useItemId"));
		data.setBattleUid(rs.getString("battleUid"));
		data.setModifyTime(rs.getLong("modifyTime"));
		data.setPatronsList(StringUtils.stringToIntegerListNoZero(rs.getString("patronsList"), ","));
		data.setWarTimes(rs.getInt("warTimes"));
		return data;
	}
}
